package com.tiemei.demo.unittest.mockito;

import static org.mockito.Mockito.*;
import static org.junit.Assert.*;

import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import com.tiemei.demo.unittest.mockito.StubConsecutiveCalls.InnerClass;


/**
 * 拿到调用者的信息
 * getArguments/getMock/getClass/getMethod
 *
 * @author tiemei.jdk
 * @date   2012-10-15
 */
public class StubWithCallbacks {
	@Test
	public void test () {
		// 1. mock
		InnerClass mock = mock(InnerClass.class);
		when(mock.someMethod(anyString())).thenAnswer(new Answer() {  
		    public Object answer(InvocationOnMock invocation) {  
		        Object[] args = invocation.getArguments();  
		        Object mock = invocation.getMock();  
		        return args[0]; 
		    }  
		});  
		  
		// 2. run
		// 3. verify
		assertEquals("foo", mock.someMethod("foo"));
	}
}
